//获得当前的时间字符串yyyy-MM-dd HH:mm:ss
function getNowTimeStr() {
    var date = new Date();
    var result = "";
    result += date.getFullYear();
    result += "-";
    result += (date.getMonth() + 1).toString().padStart(2, "0");
    result += "-";
    result += date.getDate().toString().padStart(2, "0");
    result += " ";
    result += date.getHours().toString().padStart(2, "0");
    result += ":";
    result += date.getMinutes().toString().padStart(2, "0");
    result += ":";
    result += date.getSeconds().toString().padStart(2, "0");
    return result;
}
//获得月第一天
function getMonthFirstDay(val) {
    var date = new Date(val);
    return date.getFullYear() + "-" + (date.getMonth() + 1) + "-1";
}
//获得最后一天
function getMonthLastDay(val) {
    var date = new Date(val);
    date = new Date(date.getFullYear(), date.getMonth() + 1, 0);
    return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
}
//获得时间的long值(yyyyMMddHHmmss)
function getLongTime() {
    return Date.now();
}

//from ES6
if (!String.prototype.repeat) {
    String.prototype.repeat = function (count) {
        'use strict';
        if (this == null)
            throw new TypeError('can\'t convert ' + this + ' to object');

        var str = '' + this;
        // To convert string to integer.
        count = +count;
        // Check NaN
        if (count != count)
            count = 0;

        if (count < 0)
            throw new RangeError('repeat count must be non-negative');

        if (count == Infinity)
            throw new RangeError('repeat count must be less than infinity');

        count = Math.floor(count);
        if (str.length == 0 || count == 0)
            return '';

        // Ensuring count is a 31-bit integer allows us to heavily optimize the
        // main part. But anyway, most current (August 2014) browsers can't handle
        // strings 1 << 28 chars or longer, so:
        if (str.length * count >= 1 << 28)
            throw new RangeError('repeat count must not overflow maximum string size');

        var maxCount = str.length * count;
        count = Math.floor(Math.log(count) / Math.log(2));
        while (count) {
            str += str;
            count--;
        }
        str += str.substring(0, maxCount - str.length);
        return str;
    }
}

//字符串前补充
if (!String.prototype.padStart) {
    String.prototype.padStart = function padStart(targetLength, padString) {
        targetLength = targetLength >> 0; //truncate if number, or convert non-number to 0;
        padString = String(padString !== undefined ? padString : ' ');
        if (this.length >= targetLength) {
            return String(this);
        } else {
            targetLength = targetLength - this.length;
            if (targetLength > padString.length) {
                padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
            }
            return padString.slice(0, targetLength) + String(this);
        }
    };
}

//字符串后补充
if (!String.prototype.padEnd) {
    String.prototype.padEnd = function padEnd(targetLength, padString) {
        targetLength = targetLength >> 0; // truncate if number or convert non-number to 0;
        padString = String(padString !== undefined ? padString : ' ');
        if (this.length >= targetLength) {
            return String(this);
        } else {
            targetLength = targetLength - this.length;
            if (targetLength > padString.length) {
                padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
            }
            return String(this) + padString.slice(0, targetLength);
        }
    };
}
//ES6的方法
if (!String.prototype.codePointAt) {
    (function () {
        'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
        var codePointAt = function (position) {
            if (this == null) {
                throw TypeError();
            }
            var string = String(this);
            var size = string.length;
            // `ToInteger`
            var index = position ? Number(position) : 0;
            if (index != index) { // better `isNaN`
                index = 0;
            }
            // Account for out-of-bounds indices:
            if (index < 0 || index >= size) {
                return undefined;
            }
            // Get the first code unit
            var first = string.charCodeAt(index);
            var second;
            if ( // check if it’s the start of a surrogate pair
                first >= 0xD800 && first <= 0xDBFF && // high surrogate
                size > index + 1 // there is a next code unit
            ) {
                second = string.charCodeAt(index + 1);
                if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
                    // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
                    return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
                }
            }
            return first;
        };
        if (Object.defineProperty) {
            Object.defineProperty(String.prototype, 'codePointAt', {
                'value': codePointAt,
                'configurable': true,
                'writable': true
            });
        } else {
            String.prototype.codePointAt = codePointAt;
        }
    }());
}

// IE无assign的解决办法
// 不能调用
// if (typeof Object.assign != 'function') {
//     Object.assign = function (target) {
//         'use strict';
//         if (target == null) {
//             console.log("Cannot convert undefined or null to object");
//             throw new TypeError('Cannot convert undefined or null to object');
//         }
//         target = Object(target);
//         console.log("assign");
//         for (var index = 1; index < arguments.length; index++) {
//             console.log("i="+index);
//             var source = arguments[index];
//             if (source != null) {
//                 for (var key in source) {
//                     if (Object.prototype.hasOwnProperty.call(source, key)) {
//                         target[key] = source[key];
//                     }
//                 }
//             }
//         }
//         return target;
//     };
// }
//var str = "js实现用{two}自符串替换占位符{two} {three}  {one} ".format({one: "I",two: "LOVE",three: "YOU"});
//var str2 = "js实现用{1}自符串替换占位符{1} {2}  {0} ".format("I","LOVE","YOU");
String.prototype.format = function () {
    if (arguments.length == 0) return this;
    var param = arguments[0];
    var s = this;
    if (typeof (param) == 'object') {
        for (var key in param)
            s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
        return s;
    } else {
        for (var i = 0; i < arguments.length; i++)
            s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
        return s;
    }
}

/**
 * startsWith方法
 */
if (!String.prototype.startsWith) {
    String.prototype.startsWith = function (str) {
        var reg = new RegExp("^" + str);
        return reg.test(this);
    }
}

/**
 * endsWith方法
 */
if (!String.prototype.endsWith) {
    String.prototype.endsWith = function (str) {
        var reg = new RegExp(str + "$");
        return reg.test(this);
    }
}
//替换所有
String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

//对象已定义
function defined(obj) {
    return (typeof obj != "undefined");
}

//不为空或false
function notNullOrFalse(obj) {
    return defined(obj) && obj;
}

//将字符串转为UCS2码
function transcodeToUCS2(data) {
    if (!data) return "";
    var ucs2 = "";
    for (var i = 0; i < data.length; i++) {
        ucs2 += data.codePointAt(i).toString(16).padStart(4, "0");
    }
    return ucs2;
}

//UCS2转为显示字符
function ucs2ToChar(data) {
    //每4位取值
    var r = data.match(/[0-9a-fA-F]{4}/g);
    var value = "";
    if (r == null) return value;
    //转码
    for (var i = 0; i < r.length; i++) {
        value += unescape("%u" + r[i]);
    }
    return value;
}
//固定表格头部(需要jquery)
var fixTabelHead = function (tableParentDiv) {
    var y = tableParentDiv.scrollTop;
    y = y > 0 ? y - 1 : y; //如果边框1
    //将头部向下移动滚动的距离
    //js(注意：tableParentDiv.children[0].children[0]有可能不是tr)
    // tableParentDiv.children[0].children[0].style.transform = "translateY(" + y + "px)";//
    //jquery
    $(tableParentDiv).find(".head").css("transform", "translateY(" + y + "px)");
}
//延时ms
function sleep(ms) {
    for (var t = Date.now(); Date.now() - t <= ms;);
}

/**
 * 滚动文本域到最后
 * @param {文本域对象} textarea 
 * @param {是否滚动} scrollFlag 
 */
function scrollToTextareaEnd(textarea, scrollFlag) {
    if (textarea && scrollFlag) {
        textarea.scrollTop = textarea.scrollHeight;
    }
}